using System;
using Science.Mathematics;
using L=Science.Physics.GeneralPhysics;

namespace Serway.Chapter02
{
	/// <summary>
	/// Example07: Carrier Landing
	/// A jet lands on an aircraft carrier at 140 mi/h (\sim 63 m/s).
	/// (A) What is its acceleration (assumed constant) if 
	/// it stops in 2.0 s due to an arresting cable that snags 
	/// the airplane and brings it to a stop?
	/// (B) If the plane touches down at position x_i = 0, 
	/// what is the final position of the plane?
	/// </summary>
	public class Example07
	{
		public Example07()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
		public void Compute()
		{
			L.Time t1 = new L.Time();
			L.Time t2 = new L.Time();
			t1.s = 0.0;
			t2.s = 2.0;
			L.Vector.FunctionOfTime AccelerationTime = new L.Vector.FunctionOfTime(Func);
			L.Vector.FunctionOfTime VelocityTime = new L.Vector.FunctionOfTime(Func2);
			L.Velocity vi = new L.Velocity();
			L.Position xi = new L.Position();
								
			//(A)
			vi.X = 63.0;

            Science.Mathematics.Calculus.PolynomialFunction pf = new Science.Mathematics.Calculus.PolynomialFunction();
			pf.MaximumPower = 1;
			pf.Which = 0;
			pf.Coefficient = vi.X;
			pf.Which = 1;
			pf.Coefficient = t2.s;
			pf.FindRoot();
            for(int i=0; i<pf.Root.Length; i++)
			result += Convert.ToString(pf.Root[i].Real)+"==\r\n";

			L.Acceleration a = new L.Acceleration();
            a.VectorFunctionOfTime = AccelerationTime;
		    L.Velocity v = new L.Velocity(t1,vi,a,t2);
			result += Convert.ToString(v.X)+"\r\n";
			result += Convert.ToString(v.Y)+"\r\n";
			result += Convert.ToString(v.Z)+"==\r\n";
			
			//(B)		
			xi.X = 0.0;
			L.Velocity vv = new L.Velocity();
            vv.VectorFunctionOfTime = VelocityTime;
			L.Position x = new L.Position(t1,xi,vv,t2);
			result += Convert.ToString(x.X)+"\r\n";
			result += Convert.ToString(x.Y)+"\r\n";
			result += Convert.ToString(x.Z)+"==\r\n";  
		
		}
		private L.Vector Func(L.Time t)
		{
			L.Vector a = new L.Vector();
			a.X = -63.0/2.0;
			a.Y = 0.0;
			a.Z = 0.0;
			return a;
		}
		private L.Vector Func2(L.Time t)
		{
			L.Vector v = new L.Vector();
			v.X = 63.0 - 63.0/2.0*t.s;
			v.Y = 0.0;
			v.Z = 0.0;
			return v;
		}
	}
}
